﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CpuSimulator.Instructions
{
    /// <summary>
    ///BGE(Branch Great or Equal) 如果rs1的值大于等于rs2中的值，则跳转到新地址，新地址是当前PC加上立即数的两倍
    /// _________________________________________________________________________________________________________________________________________________
    /// |___________立即数____________ |_______rs2,5位________|_________rs1,5位______|_Func3______|_________立即数_____|_____OPCODE 7位____________|
    /// | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
    /// |               imm[12|10:5]           |               rs2             |       rs1                      |    101          |        imm[4:1][11]    |            1100011                     |
    /// </summary>
    public class BGE : Instruction
    {
        public override void Execute(Cpu cpuinfo)
        {
            int address = cpuinfo.PC.Value + this.Immediate;
            if(cpuinfo.Registers[this.Rs1].Value >= cpuinfo.Registers[this.Rs2].Value){
                cpuinfo.PC.SetValue(address);
                cpuinfo.InvodeRegisterChange(cpuinfo.PC.Index, address.ToString("X8"));
            }
            else
            {
                cpuinfo.PC.SetValue(cpuinfo.PC.Value + 4);
                cpuinfo.InvodeRegisterChange(cpuinfo.PC.Index, address.ToString("X8"));
            }
        }
    }
}
